class Solution
{
public:
    int myAtoi(string str)
    {
        int i = 0;
        int flag = 1;
        long M = 0;
        while (str[i] == ' ')
            ++i;
        if (str[i] == '-')
            flag = -1;
        if (str[i] == '-' || str[i] == '+')
            ++i;
        while (isdigit(str[i]))
        {
            if ('0' <= str[i] <= '9')
            {
                M = M * 10 + (str[i] - '0');
                cout << M << endl;
            }
            if (M >= INT_MAX && flag == 1) return  INT_MAX;
            if (M > INT_MAX && flag == -1) return  INT_MIN;
            ++i;
        }
        return M * flag;
    }
};